package com.kfm.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("IndexServlet doGet()...");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("IndexServlet doPost()...");
        String username = req.getParameter("username");
        System.out.println("username: " + username);
        Object login = req.getAttribute("login");
        if (login == null){
            resp.getWriter().println("invalid request");
            return;
        } else {
            Boolean flag = (Boolean) login;
            if (flag){
                resp.getWriter().println("<h1>welcome, " + req.getAttribute("username") + " </h1>");
            } else {
                resp.getWriter().println("invalid request");
            }
        }
    }
}
